#include "ClipboardPanel.hpp"

ClipboardPanel::ClipboardPanel():Panel(0, 79, 200, 100, Panel::Static){
    title.setPosition(5, 2);
    title.setString(10, sf::Color::White, sf::Text::Underlined, "Clipboard");
    add(&title);

    info.setPosition(10 + title.labelText.getLocalBounds().width, 2);
    info.setString(10, sf::Color::White, sf::Text::Regular, std::string(""));
    add(&info);
    
    clipboardSprite.setPosition(5, 17);
    add(&clipboardSprite);

    clipboardDraw = 0;
}

/** 
* Update clipboard image. Given map and marquee selection generate image of copied area.
*/
void ClipboardPanel::updateClipboard(Map* map, SpriteManager* spriteManager, sf::Rect<int>& marquee){
    clipboard.clear();
    if(clipboardDraw != 0)
        delete clipboardDraw;
    
	//Calculate the required size and zoom scale of the clipboard.
    int width = std::abs(marquee.width);
    int height = std::abs(marquee.height);

    int left = marquee.left;
    if(width < 0) left = left - width;
    int top = marquee.top;
    if(height < 0) top = top - height;
    
    int maxWidth = 190;
    int maxHeight = 80;
    int textureHeight = height*32;
    int textureWidth = width*32;
    sf::Transform clipboardScale;
    if(textureWidth > maxWidth || textureHeight > maxHeight){
        float widthRatio = maxWidth/((float)width*32);
        float heightRatio = maxHeight/((float)height*32);
        if((widthRatio > heightRatio && widthRatio < 1 && height*32*widthRatio <= maxHeight) || heightRatio > 1){
            clipboardScale = sf::Transform().scale(widthRatio, widthRatio);
            textureWidth *= widthRatio;
            textureHeight *= heightRatio;
        }else{
            clipboardScale = sf::Transform().scale(heightRatio, heightRatio);
            textureWidth *= heightRatio;
            textureHeight *= heightRatio;
        }
    }
    
    
    info.setString(StringHelper::itos(width) + "x" + StringHelper::itos(height));
	//Create new texture.
    clipboardDraw = new sf::RenderTexture();
    clipboardDraw->create(textureWidth,textureHeight, false);
    clipboardDraw->clear(sf::Color::Transparent);

    //Copy and render marquee to clipboard.
	for(int layer = 0; layer < LAYERS; layer++){
        for(int r = 0; r < height; r++){
            for(int c = 0; c < width; c++){
                int tile = c + left + (r + top)*map->width;
                if((r + top) >= 0 && (c + left) >= 0 && (c + left) < map->width 
                        && (r + top) < map->height && map->tiles[tile][layer] != NO_SPRITE){
                    CopyTile copy;
                    copy.layer = layer;
                    copy.tile = tile;
                    copy.x = c;
                    copy.y = r;
                    copy.sprite = map->tiles[tile][layer];
                    sf::Transform position;
                    
                    position = clipboardScale;
                    position*= sf::Transform().translate(c*32, r*32);
                    clipboardDraw->draw(*spriteManager->getSprite(copy.sprite), position);
                    clipboard.push_back(copy);
                }
            }
        }
    }
    clipboardDraw->display();
    clipboardSprite.setTexture(clipboardDraw->getTexture(), true);
    
}